package neusoft.yuejian_11.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import neusoft.yuejian_11.adapter.CommentAdapter;
import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.AddPrize;
import neusoft.yuejian_11.bean.AttentionBean;
import neusoft.yuejian_11.bean.CommentBean;
import neusoft.yuejian_11.bean.IsAttentionBean;
import neusoft.yuejian_11.bean.PostBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.AttentionModel;
import neusoft.yuejian_11.model.PostModel;
import neusoft.yuejian_11.R;
import neusoft.yuejian_11.model.UserModel;

import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import static neusoft.yuejian_11.interF.Const.BASE_URI;

//晒图详情
public class AllPostDatilActivity extends BaseActivity  implements RetrofitListener {
    private ImageButton imageButtonBack;
    protected Context context;
    private int postId;
    private RecyclerView recyclerView;
    private CommentAdapter adapter;
    private TextView textPostTitle;
    private TextView textPostInfo;
    private TextView textPostTime;
    private TextView textPostPeoele;
    private TextView textPostClub;
    private ImageView p_pic;
    private ImageView p_pic1;
    private ImageView p_pic2;
    private ImageView p_pic3;
    private RoundImageView u_pic;
    private Button add;
    private EditText coinfo;
    private  ImageButton favorButton;
    private Boolean flags;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_post_datil);
        initView();
        initEvents();
        initData();


    }
    private void initEvents() {
        //添加晒图评论
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cinfos =coinfo.getText().toString();
                if (cinfos.equals("")) {
                    Toast.makeText(AllPostDatilActivity.this, "请输入评论内容", Toast.LENGTH_SHORT).show();
                } else {
                    PostModel postModel = new PostModel();
                    postModel.addcomment(getU_id(), String.valueOf(postId),cinfos,AllPostDatilActivity.this);
                }
            }
        });
        //用户收藏晒图
        favorButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttentionModel attentionModel = new AttentionModel();
                attentionModel.userCollectPost(String.valueOf(getU_id()), String.valueOf(postId),AllPostDatilActivity.this);
            }
        });
        //返回前一页
        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }

    private void initData() {
        postId = getIntent().getIntExtra("p_id",-1);
        PostModel postModel = new PostModel();
        postModel.getPostByIdDetail(String.valueOf(postId),this);
        postModel.getCommentPost(String.valueOf(postId),this);
        AttentionModel attentionModel=new AttentionModel();
        attentionModel.isCollected(String.valueOf(u_id),String.valueOf(postId), "5",this);
    }


    private void initView() {
        textPostTitle = (TextView) findViewById(R.id.textView_post_title);
        textPostInfo = (TextView) findViewById(R.id.textView_post_info);
        textPostPeoele = (TextView) findViewById(R.id.textView_post_person);
        textPostClub = (TextView) findViewById(R.id.textView_post_club);
        textPostTime = (TextView) findViewById(R.id.textView_post_time);
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        p_pic= (ImageView) findViewById(R.id.imageView);
        p_pic1= (ImageView) findViewById(R.id.imageView1);
        p_pic2= (ImageView) findViewById(R.id.imageView2);
        p_pic3= (ImageView) findViewById(R.id.imageView3);
        u_pic= (RoundImageView) findViewById(R.id.imageViewtx);
        add= (Button) findViewById(R.id.addcomment);
        coinfo= (EditText) findViewById(R.id.coinfo);
        favorButton= (ImageButton) findViewById(R.id.post_collect);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if(flag==1) {
            //晒图详情
            PostBean postBean = (PostBean) o;
            textPostTitle.setText(postBean.getP_title());
            textPostInfo.setText(postBean.getP_info());
            textPostTime.setText(postBean.getP_createtime());
            textPostPeoele.setText(postBean.getU_name());
            textPostClub.setText(postBean.getC_name());
            Picasso.with(context)
                    .load(BASE_URI+"pphoto/" + postBean.getP_pic())
                    .into(p_pic);
            Picasso.with(context)
                    .load(BASE_URI+"pphoto/" + postBean.getP_pic1())
                    .into(p_pic1);
            Picasso.with(context)
                    .load(BASE_URI+"pphoto/" + postBean.getP_pic2())
                    .into(p_pic2);
            Picasso.with(context)
                    .load(BASE_URI+"pphoto/" + postBean.getP_pic3())
                    .into(p_pic3);
            Picasso.with(context)
                    .load(BASE_URI+"photo/" + postBean.getU_pic())
                    .into(u_pic);
        }
        if (flag==2) {
            //获取评论列表
                List<CommentBean> commentBean=(List<CommentBean>)o;
                adapter = new CommentAdapter(AllPostDatilActivity.this,commentBean);
                LinearLayoutManager layoutManager = new LinearLayoutManager(AllPostDatilActivity.this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(adapter);
        }
        if (flag==5){
            //发表评论
            AddPrize addPrize=(AddPrize) o;
            if (addPrize.getSuccess().equals("0")){
                Toast.makeText(AllPostDatilActivity.this,"评论失败",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(AllPostDatilActivity.this,"评论成功",Toast.LENGTH_SHORT).show();
            }
        }
       if (flag==3){
           //判断某条晒图是否已经收藏
           IsAttentionBean isAttentionBean = (IsAttentionBean) o;;
           if (isAttentionBean.getCollected().equals("1")){
                    //已关注
                    flags = true;
                    favorButton.setImageResource(R.drawable.ic_alikepost);
                }
                else {
                    //未关注的
                    flags = false;
                    favorButton.setImageResource(R.drawable.ic_likepost);
                }
       }
       if(flag==4){
           //收藏晒图
           AttentionBean attentionBean=(AttentionBean) o;
           if (attentionBean.getSuccess().equals("1")) {
               Log.e(attentionBean.getSuccess(), "---------");
               if (flags) {
                   flags = false;
                   favorButton.setImageResource(R.drawable.ic_likepost);
               } else {
                   flags = true;
                   favorButton.setImageResource(R.drawable.ic_alikepost);

               }
               Toast.makeText(AllPostDatilActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
           }
       }


    }

    @Override
    public void onFail() {

    }


}
